//
//  ViewController.swift
//  XQDBDemo
//
//  Created by WXQ on 2021/3/10.
//

import UIKit
import SnapKit

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    private let result = "cell"
    private let tableView = UITableView.init(frame: .zero, style: .plain)
    private var dataArr = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = "Database Demo"
        self.initTableView()
        self.dataArr = [
            "CoreData",
            "FMDB",
            "Realm",
            "WCDB",
        ]
    }
    
    private func initTableView() {
        self.view.addSubview(self.tableView)
        self.tableView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        
        self.tableView.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: result)
        
        self.tableView.delegate = self
        self.tableView.dataSource = self
        
        self.tableView.tableFooterView = UIView()
    }
    
    // MARK: - UITableViewDataSource
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.dataArr.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: result, for: indexPath)
        cell.textLabel?.text = self.dataArr[indexPath.row]
        return cell
    }
    
    // MARK: - UITableViewDelegate
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        switch indexPath.row {
        
        case 0:
            self.navigationController?.pushViewController(XQCoreDataVC(), animated: true)
            
        case 1:
            self.navigationController?.pushViewController(XQFMDBVC(), animated: true)
            
        case 2:
            self.navigationController?.pushViewController(XQRealmVC(), animated: true)
            
        case 3:
            self.navigationController?.pushViewController(XQWCDBVC(), animated: true)
            
        default:
            break
        }
    }


}

